﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Joyintech.Common;
using Joyintech.Model.inpiao365;

namespace Joyintech.Admin.inpiao365.core
{
    public partial class selectAttachment : System.Web.UI.Page
    {
        #region 变量定义
        
        /// <summary>
        /// 传入的文件夹路径
        /// </summary>
        protected string folderPath = string.Empty;
        
        /// <summary>
        /// 当前显示文件夹
        /// </summary>
        protected string currentFolder = string.Empty;

        /// <summary>
        /// 文件(文件夹)操作类
        /// </summary>
        FileOperate fileManage = new FileOperate();

        #endregion

        #region 页面加载

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                if (base.Request.QueryString["Path"] != null)
                {
                    this.folderPath = base.Server.UrlDecode(base.Request.QueryString["Path"].ToString());
                }
                if (string.IsNullOrEmpty(this.folderPath))
                {
                    this.folderPath = base.Server.MapPath("/Uploads");
                    this.CreateCurrentMonthDir();
                }
                else
                {
                    this.folderPath = base.Server.MapPath("/" + this.folderPath);
                }
                List<FileFolderInfo> files = this.fileManage.GetDirectories(this.folderPath);
                int folderNum = this.fileManage.FolderNum;
                int fileNum = this.fileManage.FileNum;
                this.rptFileList.DataSource = files;
                this.rptFileList.DataBind();
                this.currentFolder = this.folderPath.ToString().Replace(base.Server.MapPath("/"), "");
                this.linkCurrent.Text = this.currentFolder;
                this.linkCurrent.NavigateUrl = "selectAttachment.aspx?path=" + this.currentFolder;
                this.linkAddFile.NavigateUrl = "uploadImages.aspx?path=" + this.currentFolder;
            }
        }

        #endregion

        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, ImageClickEventArgs e)
        {
            base.Response.Redirect("selectAttachment.aspx?path=" + Utils.GetCookie(this.Page.Request.FilePath));
        }

        /// <summary>
        /// 向上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUp_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Server.MapPath("/" + this.linkCurrent.Text) != base.Server.MapPath("/Uploads"))
            {
                Utils.WriteCookie(this.Page.Request.FilePath, this.linkCurrent.Text);
                DirectoryInfo dir = new DirectoryInfo(base.Server.MapPath("/" + this.linkCurrent.Text));
                string parentFolder = dir.Parent.FullName.Replace(base.Server.MapPath("/"), "");
                base.Response.Redirect("selectAttachment.aspx?path=" + base.Server.UrlEncode(parentFolder));
            }
        }

        /// <summary>
        /// 创建当前“年月”文件夹
        /// </summary>
        private void CreateCurrentMonthDir()
        {
            string DirectoryPath = base.Server.MapPath("/Uploads") + @"\\" + DateTime.Now.ToString("yyyy-MM");
            if (!Directory.Exists(DirectoryPath))
            {
                Directory.CreateDirectory(DirectoryPath);
            }
        }

    }
}